; LOADER FOR DEMO : MAURICE ADORE LES BOBS
; 10/01/2006	v1.0 - Extracted from the main source for a better memory management (I don't need to keep the loader after starting the demo)
; 11/01/2006	code optimization
;		add a vbl synchro tempo - maybe not needed when crunched version will be used...
; todo - add the loading or decrunching of the loader instro screen by SuperSylvestre
;
	org &300
nolist
	jp goooo_LOADER
; ---------------------------
; CONSTANTS
; ---------------------------
MAURICE_LOADS1_BANK	equ &c0
MAURICE_LOADS1_ADDRESS	equ &C000
MAURICE_MAIN_BANK	equ &c0
MAURICE_MAIN_ADDRESS	equ &1A00-3
MAURICE_CLEAR_BANK	equ &c4
MAURICE_CLEAR_ADDRESS	equ &4000
MAURICE_DRAW_BANK	equ &c5
MAURICE_DRAW_ADDRESS	equ &4000
MAURICE_TEXT_BANK	equ &c7
MAURICE_TEXT_ADDRESS	equ &4000
MAURICE_PLAYER_BANK	equ &c6
MAURICE_PLAYER_ADDRESS	equ &4000
MAURICE_MUSIC_ADDRESS	equ &4800
MAURICE_FONT_BANK	equ MAURICE_MAIN_BANK
MAURICE_FONT_ADDRESS	equ &3c4e
; ---------------------------
; DRAW_BACKGROUND_SCREEN  equ &75c8
; ---------------------------
	read "Clear_Bob1_3_0.asm"
; ---------------------------
goooo_LOADER
	LOADER	equ 0		; set the loader mode (loading or decrunching)

	IF LOADER

	LD HL,(#BE7D)	; Adresses variables Amsdos
	LD A,(HL)	; Lecteur courant
	PUSH AF		; Sauvegarde lecteur courant
	LD C,7
	LD DE,#40
	LD HL,#ABFF
	CALL #BCCE	; Initalisation ROM DISC
	POP AF
	LD HL,(#BE7D)
	LD (HL),A	; Restore lecteur courant

	ENDIF

	call COLORS_TO_BLACK

	ld hl,table_maurice_rupture
	call set_rupture

	exx
	push hl
	push de
	push bc
	exx

	di

	ld de,&c000
	call MAURICE_DRAW_TITLE_TEXTURE

	exx
	pop bc
	pop de
	pop hl
	exx

	ei

	ld hl,MAURICE_TITLE_ADDRESS
	ld de,&E080+10
	ld bc,&644b
DRAW_TITLE_LOOP
	push bc
	push de
	ld b,c
DRAW_TITLE_MASK_LOOP
	push bc
	ld a,(hl)
	ld c,a
	ld b,&FF
	and b
	jr z,DRAW_TITLE_NOTHING_TO_DO
	ld a,c
	ld b,&EE
	and b
	jr z,DRAW_TITLE_MASK
	ld a,c
	ld b,&CC
	and b
	jr z,DRAW_TITLE_MASK
	ld a,c
	ld b,&88
	and b
	jr z,DRAW_TITLE_MASK
	ld a,c
	ld b,&77
	and b
	jr z,DRAW_TITLE_MASK
	ld a,c
	ld b,&33
	and b
	jr z,DRAW_TITLE_MASK
	ld a,c
	ld b,&11
	and b
	jr z,DRAW_TITLE_MASK
DRAW_TITLE_NO_MASK
	ld b,0

DRAW_TITLE_MASK
	ld a,(de)
	and b
	or c
	ld (de),a
DRAW_TITLE_NOTHING_TO_DO
	inc hl
	inc de
	pop bc
	djnz DRAW_TITLE_MASK_LOOP
	pop de
	ld a,8
	add a,d
	ld d,a
	and &38
	jp nz,DRAW_TITLE_NO_NEXTLINE
	ex de,hl
	ld bc,&c080
	add hl,bc
	ex de,hl
DRAW_TITLE_NO_NEXTLINE
	pop bc
	djnz DRAW_TITLE_LOOP

DRAW_TITLE_END
	ld a,0
	ld bc,&0505
	call &bc32

	ld a,1
	ld bc,&1515
	call &bc32

	ld a,2
	ld bc,&0707
	call &bc32

	ld a,3
	ld bc,&0404
	call &bc32

	ld bc,&0000
	call &bc38

	ld de,&0050
loop_wait
	ld b,&f5
SYNCHRO_TEMPO
	in a,(c)
	rra
	jr nc,SYNCHRO_TEMPO

	ei
	HALT
	HALT
	di

	dec de
	ld a,d
	or e
	jr nz,loop_wait

	IF LOADER

	ld hl,MAURICE_LOADING_TABLE
MAURICE_LOADING_LOOP
	ld a,(hl)
	or a		;cp 0
	jr z,MAURICE_END_LOADING

	ld b,&7f
	out (c),a

	inc hl
	ld e,(hl)
	inc hl
	ld d,(hl)
	inc hl

	push hl
	call LOAD_FILE
	pop hl

	ld bc,12
	add hl,bc

	jr MAURICE_LOADING_LOOP
; ---------------------------
LOAD_FILE
; ---------------------------
; HL = FILENAME PTR
; DE = BUFFER ADDRESS
; ---------------------------
        ld b,12
        push de
        call &bc77
	jr nc,erreur_load
        pop hl
        call &bc83
	jr nc,erreur_load
        call &bc7a
	jr nc,erreur_load
	ret
erreur_load
	jp &bc7d

MAURICE_END_LOADING

	ELSE

	ld hl,MAURICE_DECRUNCH_TABLE
MAURICE_DECRUNCH_LOOP
	ld bc,&7f00+MAURICE_MAIN_BANK
	out (c),c

	ld a,(hl)
	or a		;cp 0
	jr z,MAURICE_END_DECRUNCH
	inc hl
	ld e,(hl)	; de = begin of data to decrunch
	inc hl
	ld d,(hl)
	inc hl
	ld (MAURICE_DECRUNCH_DATA_SOURCE+1),de
	ld e,(hl)	; de = destination of data to decrunch
	inc hl
	ld d,(hl)
	inc hl
	ld (MAURICE_DECRUNCH_DATA_DEST+1),de
	ld e,(hl)	; de = length of data to decrunch
	inc hl
	ld d,(hl)
	inc hl
	ld (MAURICE_DECRUNCH_DATA_LENGTH+1),de
	ld (MAURICE_DECRUNCH_DATA_LENGTH1+1),de
	push hl
MAURICE_DECRUNCH_DATA_SOURCE
	ld hl,0
	ld de,&8700
MAURICE_DECRUNCH_DATA_LENGTH1
	ld bc,0
	ldir

	ld b,&7f
	out (c),a

	ld hl,&8700
MAURICE_DECRUNCH_DATA_DEST
	ld de,0
MAURICE_DECRUNCH_DATA_LENGTH
	ld bc,0
	ldir

	pop hl
	ld e,(hl)
	inc hl
	ld d,(hl)
	inc hl
	ld (MAURICE_DECRUNCH_CALL+1),de
	push hl
MAURICE_DECRUNCH_CALL
	CALL 0
	pop hl
	jr MAURICE_DECRUNCH_LOOP
MAURICE_END_DECRUNCH

	ENDIF

	ei
	call COLORS_TO_BLACK
	di

	ld hl,&c9fb		; set new interrupt
	ld (&38),hl

	ld bc,&7f00+MAURICE_PLAYER_BANK
	out (c),c
	ld a,%10011101
	out (c),a	; Set Interrupt and Upper/Lower rom area disable

	call MAURICE_PLAYER_ADDRESS

	ld bc,&7f00+MAURICE_DRAW_BANK
	out (c),c

	ld sp,&100
	call DRAW_BACKGROUND_SCREEN
	
	ld bc,&7f00+MAURICE_MAIN_BANK
	out (c),c

	ld hl,&4000
	ld (hl),&00
	ld de,&4001
	ld bc,&3fff
	ldir

; &58 , &43
; &47 , &54

	ld hl,&5843	; Set the colors and mode
	ld bc,&7f00
	out (c),c
	out (c),h
	inc c
	out (c),c
	out (c),l
	inc c
	out (c),c
	ld a,&47
	out (c),a
	inc c
	out (c),c
	ld a,&54
	out (c),a
	ld a,&10
	out (c),a
	out (c),h

	jp MAURICE_MAIN_ADDRESS

set_rupture
	ld a,(hl)
	inc hl
	ld b,&bd
set_crtc_regs
	outi
	inc b
	inc b
	outi
	dec a
	jr nz,set_crtc_regs
	ret

table_maurice_rupture
	defb 10
	defb 00,&40
	defb 01,&40
	defb 02,&33
	defb 03,&8e
	defb 05,&00
	defb 06,&10
	defb 07,&1d
	defb 09,&07
	defb 12,&30
	defb 13,&00

	IF LOADER

MAURICE_LOADING_TABLE
	defb MAURICE_CLEAR_BANK
	defw MAURICE_CLEAR_ADDRESS
	defb "MAURICE0.BIN"
	defb MAURICE_DRAW_BANK
	defw MAURICE_DRAW_ADDRESS
	defb "MAURICE1.BIN"
	defb MAURICE_PLAYER_BANK
	defw MAURICE_PLAYER_ADDRESS
	defb "MAURICE .PYM"
	defb MAURICE_PLAYER_BANK
	defw MAURICE_MUSIC_ADDRESS
	defb "MAURICE .AYC"
	defb MAURICE_TEXT_BANK
	defw MAURICE_TEXT_ADDRESS
	defb "MAURICE .TXT"
	defb MAURICE_MAIN_BANK
	defw MAURICE_MAIN_ADDRESS
	defb "MAURICE .BIN"
	defb MAURICE_FONT_BANK
	defw MAURICE_FONT_ADDRESS
	defb "MAURICE .FNT"
	defb 0

	ELSE

; begin of data
; destination data to decrunch
; length of data
; decrunch excution address

MAURICE_DECRUNCH_TABLE
	defb MAURICE_PLAYER_BANK
	defw &416A, &446e, &0468, &4870
	defb MAURICE_PLAYER_BANK
	defw &45d2, &4c11, &0bcd, &5778
	defb MAURICE_TEXT_BANK
	defw &519f, &511f, &0db3, &5e6c
	defb MAURICE_CLEAR_BANK
	defw &7651, &797f, &0cf4, &860d
	defb MAURICE_DRAW_BANK
	defw &5f53, &6b70, &16fd, &8207
	defb MAURICE_MAIN_BANK
	defw &3500, &3a53, &0c67, &4654
	defb 0
;

	ENDIF

COLORS_TO_BLACK
	ld b,16
SET_COLORS_TO_BLACK
	push bc
	ld a,b		; color a,0
	dec a
	ld bc,&0000
	call &bc32
	pop bc
	djnz SET_COLORS_TO_BLACK

	ld bc,&0000	; border 0
	call &bc38

	ld a,1		; mode 1
	jp &bc0e

BACKGROUND_SCREEN_DATA
	defb &F0, &F0, &F0, &F0, &F0, &F0, &F0, &F0
	defb &F7, &FF, &FF, &FF, &FF, &FF, &FF, &FF
	defb &E4, &E5, &29, &A8, &AC, &D7, &F5, &95
	defb &E5, &7C, &C8, &C9, &FB, &B5, &D7, &93
	defb &C6, &4E, &9C, &CB, &BF, &1B, &1B, &1D
	defb &D7, &4F, &0B, &AF, &8A, &5F, &15, &1B
	defb &F7, &FF, &FF, &FF, &FF, &FF, &FF, &FF
	defb &F0, &F0, &F0, &F0, &F0, &F0, &F0     
BACKGROUND_SCREEN_DATA_END
	defb &F0
; ---------------------------
BACKGROUND_SCREEN_DATA1
	defb &F0, &F0, &F0, &F0, &F0, &F0, &F0, &F0
	defb &FF, &FF, &FF, &FF, &F7, &FF, &FF, &FF
	defb &AC, &D7, &F5, &95, &E4, &E5, &29, &A8
	defb &FB, &B5, &D7, &93, &E5, &7C, &C8, &C9
	defb &BF, &1B, &1B, &1D, &C6, &4E, &9C, &CB
	defb &8A, &5F, &15, &1B, &D7, &4F, &0B, &AF
	defb &FF, &FF, &FF, &FF, &F7, &FF, &FF, &FF
	defb &F0, &F0, &F0, &F0, &F0, &F0, &F0
BACKGROUND_SCREEN_DATA_END1
	defb &F0
BACKGROUND_SCREEN_DATA_CLEAR
	defb &00, &00, &00, &00, &00, &00, &00
BACKGROUND_SCREEN_DATA_CLEAR_END
	defb &00
; ---------------------------
DRAW_BACKGROUND_SCREEN
	ld de,&ffff
	call DRAW_BACKGROUND_SCREEN_MAIN
	exx
DRAW_BACKGROUND_SCREEN_MAIN
	ld hl,BACKGROUND_SCREEN_DATA_END
	ld (do_one_line+1),hl
	ld hl,BACKGROUND_SCREEN_DATA_END1
	ld (do_one_line1+1),hl
	exx
	ld b,8
	ld de,64
do_all_line
	exx
	ld b,4
do_16_lines
	ld a,b

	ld bc,16*8
do_one_line_CLEAR
	ld hl,BACKGROUND_SCREEN_DATA_CLEAR_END
	ldd
	ldd
	ldd
	ldd
	ldd
	ldd
	ldd
	ldd
	jp pe,do_one_line_CLEAR

	ld bc,16*8
do_one_line1
	ld hl,BACKGROUND_SCREEN_DATA_END1
	ldd
	ldd
	ldd
	ldd
	ldd
	ldd
	ldd
	ldd
	jp pe,do_one_line1

	ld bc,16*8
do_one_line_CLEAR1
	ld hl,BACKGROUND_SCREEN_DATA_CLEAR_END
	ldd
	ldd
	ldd
	ldd
	ldd
	ldd
	ldd
	ldd
	jp pe,do_one_line_CLEAR1

	ld bc,16*8
do_one_line
	ld hl,BACKGROUND_SCREEN_DATA_END
	ldd
	ldd
	ldd
	ldd
	ldd
	ldd
	ldd
	ldd
	jp pe,do_one_line

	ld b,a
	djnz do_16_lines
	
	ld (do_one_line+1),hl
	exx
	ld hl,(do_one_line+1)
	add hl,de
	ld (do_one_line1+1),hl

	djnz do_all_line
	ret

MAURICE_DRAW_TITLE_TEXTURE

	ld hl,MAURICE_TITLE_TEXTURE_ADDRESS
	ld (do_one_line_texture+1),hl
	exx
	ld b,8
do_all_line_texture
	exx
	ld b,16
do_16_lines_texture
	ld a,b
	ld bc,12*10
do_one_line_texture
	ld hl,MAURICE_TITLE_TEXTURE_ADDRESS
	ldi
	ldi
	ldi
	ldi
	ldi
	ldi
	ldi
	ldi
	ldi
	ldi
	jp pe,do_one_line_texture

do_rest_of_one_line_texture
	ld hl,MAURICE_TITLE_TEXTURE_ADDRESS
	ldi
	ldi
	ldi
	ldi
	ldi
	ldi
	ldi
	ldi

	ld b,a
	djnz do_16_lines_texture

	inc hl
	inc hl
	ld (do_one_line_texture+1),hl
	ld (do_rest_of_one_line_texture+1),hl

	exx
	djnz do_all_line_texture
	ret

MAURICE_TITLE_TEXTURE_ADDRESS
	incbin "MAURICE_TEXTURE.WIN"

	org &8400
MAURICE_TITLE_ADDRESS
	incbin "MAURICE_TITLE1.WIN"
list
; end of file
EOF_ADDRESS
EOF_LENGTH equ EOF_ADDRESS-&300

